<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <el-button type="primary" class="handle-del mr10" @click="handleSearch"
          >查询</el-button
        >

        <el-button type="primary" @click="handleExport">导出</el-button>
        <el-button
          type="danger"
          class="handle-del mr10"
          @click="delAllSelection"
          v-if="pmPermissions.includes('wx:suggest:delete')"
          >删除</el-button
        >

        <!-- <el-select
          v-model="query.address"
          placeholder="地址"
          class="handle-select mr10"
        >
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select> -->
        <!-- <el-input
          v-model="query.name"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input> -->
        <!-- <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        > -->
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="createDate"
          align="center"
          label="时间"
        ></el-table-column>

        <el-table-column
          prop="nickname"
          align="center"
          label="昵称"
        ></el-table-column>

        <el-table-column
          prop="mobile"
          align="center"
          label="手机号"
        ></el-table-column>

        <el-table-column
          prop="content"
          align="center"
          label="内容"
        ></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="pmPermissions.includes('wx:suggest:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import NProgress from "nprogress";
export default {
  name: "proposaimanage",
  data() {
    return {
      pmPermissions: [],
      query: {
        pageIndex: 1,
        pageSize: 20,
      },
      value2: "",
      tableData: [],
      multipleSelection: [],
      delList: [],
      pageTotal: 0,
      idx: -1,
      id: -1,
      ids: [],
    };
  },
  created() {
    this.getData();
    this.pmPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/proposal-manage") {
        this.pmPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.pmPermissions = newVal;
    },
  },
  methods: {
    //导出
    handleExport() {
      NProgress.start();
      this.$axios
        .get("/wx/suggest/export", {
          params: {
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
          responseType: "blob",
        })
        .then((res) => {
          console.log(res);
          if (res.data.type != "application/vnd.ms-excel") {
            NProgress.done();
            return this.$message.warning({
              message: res.msg,
              duration: 1500,
            });
          }

          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "建议_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);

          NProgress.done();
        })
        .catch((err) => {});
    },
    getData() {
      this.$axios
        .get("/wx/suggest/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
        })
        .then(({ data: res }) => {
          console.log("建议管理", res);
          if (res.code != 0) {
            return;
          }
          this.pageTotal = res.data.total || 50;
          res.data.list.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
            item.createDate = this.dateFormat(item.createDate);
          });
          this.tableData = res.data.list;
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },

    delete(ids) {
      console.log(ids);
      this.$axios
        .delete("/wx/suggest", {
          data: ids,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.ids = [];
          this.getData();
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      console.log(val);
      this.ids = [];
      var ids = this.ids;
      val.forEach((item, index) => {
        ids.push(item.id);
      });
    },
    delAllSelection() {
      // 二次确认删除
      var ids = this.ids;

      ids = [...new Set(ids)];
      console.log(ids);
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
